package desmontec.documentos

import desmontec.funcionarios.*;
import desmontec.query.dpg.DPGQuery;

class DPGController {
	
	// arthur.marques - INICIO	
	// Ele injeta automaticamente o service no controller
	DpgService dpgService
	// arthur.marques - FIM

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [
			DPGInstanceList: DPG.list(params),
			DPGInstanceTotal: DPG.count()						
		]
    }

    def create = {
        def DPGInstance = new DPG()
        DPGInstance.properties = params
        return [DPGInstance: DPGInstance]
    }

    def save = {
        def DPGInstance = new DPG(params)
        if (DPGInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'DPG.label', default: 'DPG'), DPGInstance.id])}"
            redirect(action: "show", id: DPGInstance.id)
        }
        else {
            render(view: "create", model: [DPGInstance: DPGInstance])
        }
    }

    def show = {
        def DPGInstance = DPG.get(params.id)
        if (!DPGInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'DPG.label', default: 'DPG'), params.id])}"
            redirect(action: "list")
        }
        else {
            [DPGInstance: DPGInstance]
        }
    }

    def edit = {
        def DPGInstance = DPG.get(params.id)
        if (!DPGInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'DPG.label', default: 'DPG'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [DPGInstance: DPGInstance]
        }
    }

    def update = {
        def DPGInstance = DPG.get(params.id)
        if (DPGInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (DPGInstance.version > version) {
                    
                    DPGInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'DPG.label', default: 'DPG')] as Object[], "Another user has updated this DPG while you were editing")
                    render(view: "edit", model: [DPGInstance: DPGInstance])
                    return
                }
            }
            DPGInstance.properties = params
            if (!DPGInstance.hasErrors() && DPGInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'DPG.label', default: 'DPG'), DPGInstance.id])}"
                redirect(action: "show", id: DPGInstance.id)
            }
            else {
                render(view: "edit", model: [DPGInstance: DPGInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'DPG.label', default: 'DPG'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def DPGInstance = DPG.get(params.id)
        if (DPGInstance) {
            try {
                DPGInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'DPG.label', default: 'DPG'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'DPG.label', default: 'DPG'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'DPG.label', default: 'DPG'), params.id])}"
            redirect(action: "list")
        }
    }
	
	// arthur.marques - INICIO
	def procurar = {
		
		DPGQuery query = new DPGQuery();
		
		bindData(query, params)
		
		if (request.method == 'POST'){
			render(view:'list', model:[
				DPGInstanceList: dpgService.consultarDPG(query),
				DPGInstanceTotal: DPG.count()				
			])
		}
		[listaEngenheiros: Engenheiro.list()]
	}
	// arthur.marques - FIM
	
}
